import baseUrl from "./url.js"

const request = (url, data) => {
    return new Promise((resolve, reject) => {
        const token = uni.getStorageSync("token") || ""
        const language = uni.getStorageSync("language") || "En"
        const httpDefaultOpts = {
            url: baseUrl.url + url,
            data,
            method: "POST",
            dataType: "json",
            header: {
                "x-requested-with": "XMLHttpRequest",
                "content-type": "application/x-www-form-urlencoded",
                "Authorization": token,
            }
        }
        uni.request(httpDefaultOpts).then(async res => {
            if (res.statusCode === 200) {
                if (res.data.code === 0) {
                    resolve(res.data)
                } else {
                    console.log(res.data)
                    resolve(res.data)
                }
            } else {
                console.log("发生错误的URL", url, data)
                reject(res.data)
            }
        }).catch((response) => {
            console.log("发生错误的URL", url, data)
            reject(response)
        })
    })
}

export default request
